package com.budwk.app.enterprise.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.budwk.app.enterprise.models.AssetLiabilityCategory;
import com.budwk.app.enterprise.services.AssetLiabilityCategoryService;
import com.budwk.starter.common.openapi.annotation.*;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@IocBean
@At("/platform/enterprise/asset/liability/category")
@ApiDefinition(tag = "资产负债类")
@Slf4j
public class AssetLiabilityCategoryController {
    @Inject
    private AssetLiabilityCategoryService assetLiabilityCategoryService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询资产负债类")
    @ApiFormParams(implementation = AssetLiabilityCategory.class)
    @ApiResponses(implementation = AssetLiabilityCategory.class)
    @SaCheckPermission("enterprise.asset.liability.category.listByPage")
    public Result<?> list(@Param("..")AssetLiabilityCategory category) {
        Pagination pagination = assetLiabilityCategoryService.listByPage(category);
        return Result.data(pagination);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增资产负债类")
    @ApiFormParams(
        implementation = AssetLiabilityCategory.class
    )
    @ApiResponses
    @SLog(tag = "新增资产负债类", value = "${args[0].id}")
    @SaCheckPermission("enterprise.asset.liability.category.create")
    public Result<?> create(@Param("..")AssetLiabilityCategory dfxyEnterprise, HttpServletRequest req) {
        dfxyEnterprise.setCreatedBy(SecurityUtil.getUserId()).setCreatedAt(LocalDateTime.now())
                .setUpdatedAt(LocalDateTime.now()).setUpdatedBy(SecurityUtil.getUserId());
        assetLiabilityCategoryService.insert(dfxyEnterprise);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改资产负债类")
    @ApiFormParams(
        implementation = AssetLiabilityCategory.class
    )
    @ApiResponses
    @SLog(tag = "修改资产负债类", value = "${args[0].id}")
    @SaCheckPermission("enterpris.asset.liability.category.update")
    public Result<?> update(@Param("..")AssetLiabilityCategory dfxyEnterprise, HttpServletRequest req) {
        dfxyEnterprise.setUpdatedBy(SecurityUtil.getUserId()).setUpdatedAt(LocalDateTime.now());
        assetLiabilityCategoryService.updateIgnoreNull(dfxyEnterprise);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取资产负债类")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckPermission("enterprise.asset.liability.category.getData")
    public Result<?> getData(@Param("id")String id, HttpServletRequest req) {
        AssetLiabilityCategory dfxyEnterprise = assetLiabilityCategoryService.fetch(id);
        if (dfxyEnterprise == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(dfxyEnterprise);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除资产负债类")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SLog(tag = "删除资产负债类", value = "${args[0]}")
    @SaCheckPermission("enterprise.asset.liability.category.delete")
    public Result<?> delete(@Param("id")String id) {
        AssetLiabilityCategory dfxyEnterprise = assetLiabilityCategoryService.fetch(id);
        if (dfxyEnterprise == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        assetLiabilityCategoryService.delete(id);
        return Result.success();
    }
}
